// packageD/energy/powerEquipment/powerEquipment.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    rewardMoneyBtn: false,
    page:1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // this.getdata()
  },

  toRecord(e){
    let id = e.currentTarget.dataset.id;
    console.log(id);
    wx.navigateTo({
      url: '/packageE/energy/powerNotes/powerNotes?id='+id,
    });
  },
  Delete(e){
    let id = e.currentTarget.dataset.id;
    let urlStr = app.getNetAddresss("plugin.energy-cabin.frontend.dealer-terminal.del-equipment");
    let that = this;
    let json = {
      id:id
    };
    app._getNetWork({
      url: urlStr,
      data: json,
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          console.log(res);
          wx.showToast({
            title: res.msg,
            icon:'none',
            duration:1000
          });
          that.getdata();
        }else{
          wx.showToast({
            title: res.msg,
            icon:'none',
            duration:1000
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  swichTabTItem(e) {
    console.log(e);
  },
  bindEquipment(){
    wx.navigateTo({
      url: '/packageE/energy/bindEquipment/bindEquipment',
    });
  },
  taperwei(e) {
    let info = e.currentTarget.dataset.info;
    console.log(info);
    this.setData({
      rewardMoneyBtn: true,
      codeimg:info.qr_code_url
    });
  },
  saveImg() {
    wx.getSetting({
      success: (res) => {
        if (!res.authSetting['scope.writePhotosAlbum']) {
          wx.authorize({
            scope: 'scope.writePhotosAlbum',
            success() {}
          });
        }
      }
    });
    var imgSrc = this.data.codeimg;
    wx.downloadFile({
      url: imgSrc,
      success: function(res) {
        // 图片保存到本地
        wx.saveImageToPhotosAlbum({
          filePath: res.tempFilePath,
          success: function(data) {
            wx.showToast({
              title: '保存成功',
              icon: 'none',
              duration: 2000
            });
          },
          fail: function(err) {
            if (err.errMsg === 'saveImageToPhotosAlbum:fail auth deny') {
              wx.openSetting({
                success(settingdata) {
                  if (settingdata.authSetting['scope.writePhotosAlbum']) {
                    wx.showToast({
                      icon: 'none',
                      title: '获取权限成功,再次长按保存图片',
                      duration: 1500
                    });
                  } else {
                    wx.showToast({
                      icon: 'none',
                      title: '获取权限失败,保存图片需要用户授权',
                      duration: 1500
                    });
                  }
                }
              });
            }
          },
          complete(res) {
            console.log(res);
          }
        });
      }
    });
  },
  onClose(e) {
    this.setData({
      rewardMoneyBtn: false
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getdata();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  getdata() {
    let urlStr = app.getNetAddresss("plugin.energy-cabin.frontend.dealer-terminal.get-equipment-list");
    let that = this;
    let json = {
      page: this.page
    };
    app._getNetWork({
      url: urlStr,
      data: json,
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          console.log(res);
          res.data.data.forEach((item, index, key) => {
            item.checked = false;
            item.info = {};
          });
          that.setData({
            datas: res.data.data,
            current_page: res.data.current_page,
            last_page: res.data.last_page,
            total: res.data.total
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  expand(e) {
    
    let item = e.currentTarget.dataset.info;
    let ind = e.currentTarget.dataset.ind;
    let that = this;
    if (!item.checked) {
      wx.showLoading({
        title: '加载中',
      });
      let json = {
        id: item.id
      };
      let urlStr = app.getNetAddresss("plugin.energy-cabin.frontend.dealer-terminal.get-device-data");
      let that = this;
      app._getNetWork({
        url: urlStr,
        data: json,
        success: function (resdata) {
          wx.hideLoading({
            success: (res) => {},
          });
          var res = resdata.data;
          if (res.result == 1) {
            console.log(res);
            for(let i = 0;i<that.data.datas.length;i++){
              that.data.datas[ind].info = res.data;
              that.data.datas[ind].checked = !item.checked;
            }
            that.setData({
              datas:that.data.datas
            });
          }
        },
        fail: function (res) {
          wx.hideLoading({
            success: (res) => {},
          });
          console.log(res);
        }
      });
    }else{
      for(let i = 0;i<that.data.datas.length;i++){
        that.data.datas[ind].checked = !item.checked;
      }
      that.setData({
        datas:that.data.datas
      });
    }
  }
});